<?php
/* 
 * ebiz front
 * 
 * count shipping fee Service
 * 
 * @author allen
 * 
 */
require_once '../app/constants/ConfigConstants.php';
require_once '../app/services/PromotionImplService.php';
require_once '../app/constants/Order.php';

class ShippingFeeService extends BaseService {
	
	private $configDao;
	private $productItemDao;
	
    public function __construct(){
        parent::__construct();
        $this->configDao = new ConfigDao($this->dbUtil);
        $this->productItemDao = new ProductItemDao($this->dbUtil);
    }
	
	public function getShippingFee($itemIds, $qtyArray, $allTotal){ //$allTotal = allItem ( dealprice * qty) 
	   	//get the promotion service to get which shipping method is free shipping 
		$promotionImplService = new PromotionImplService();
	   	//get which shipping method is free shipping
	   	$freeShippingMethod = $promotionImplService->processFreeShipping($allTotal); //0 is all shipping method is charging
		$shipPriceInfo = array();
	   	$rateInfoArray = array();
	   	$confKey = ConfigConstants::$shippingRate; //out put is "shipping_rate"
//	   	$shippingExpectList = Order::getShippingExpectList(); //get the shipping rate name(label) from constants
	   	$orderService = new OrderService();
//	   	$shippingExpectList = Order::getShippingExpectListFromDB(); //get the shipping rate name(label) from constants
	   	$shippingExpectList = $orderService->getShippingExpectListFromDB(); //get the shipping rate name(label) from constants
	   	$rateInfo = $this->configDao->getShipInfoByConfKey($confKey); //get the shipping rate price from DB
	   	for ($i = 0; $i < count($rateInfo); $i++){
	   		$rateInfoArray[$rateInfo[$i]['conf_key']] = $rateInfo[$i]['conf_value']; //format the array to save the shipping key and value    
	   	}
        $itemInfoArray = $this->productItemDao->getByIds($itemIds); //get the item info by item id
	   	foreach ($shippingExpectList as $shippingExpectKey => $shippingExpectValue) { //while the all rate sort
	   		$shippingPrice = 0; //init the data
            $rate = $rateInfoArray[$confKey . "_" . $shippingExpectKey]; //get the each shipping rate price
            for($j = 0; $j < count($itemInfoArray); $j++){ //while the all order item
                $freeShippingStatus = $itemInfoArray[$j]['free_shipping_status']; //get the shipping status,1 is can free shipping goods
            	$shipType = $itemInfoArray[$j]['shipping_type']; //get the item ship type
                $weight = $itemInfoArray[$j]['weight']; //get the item weight
                $qty = $qtyArray[$j]; //get the item quantity
                //if the shipping method is free shipping and this item can free shipping
                if($freeShippingStatus == 1 && $freeShippingMethod == $shippingExpectKey){ 
                	$shippingPrice += 0;
                }else{
                    $shippingPrice += self::countShipFeeByTypeAndWeight($shipType, $weight, $qty, $rate); //return the ship price of one item
                }
            }
            $shippingPrice = number_format($shippingPrice, 2);
			$desc = "";
			$shipPriceInfo[$shippingExpectKey] = array("shippingExpectKey" => $shippingExpectKey, 
			                                           "shippingExpectValue" => $shippingExpectValue, 
			                                           "shippingPrice" => $shippingPrice, 
			                                           "desc" => $desc,
			                                           "unit" => UNIT);
	   	}
	   	return $shipPriceInfo;
	}
	
	/**
	 * $itemIds is string and separation by ','
	 * $isFreeShippingArray is from promotionImplService in json 
	 */
	public function getOneShippingMethodFee($itemIds, $qtyArray, $allTotal, $shippingMethodKey, $isFreeShippingArray){//only count one method shipping method fee   
        //get the promotion service to get which shipping method is free shipping 
        $promotionImplService = new PromotionImplService();
        //get which shipping method is free shipping
        $freeShippingMethod = $promotionImplService->processFreeShipping($allTotal); //0 is all shipping method is charging
        $shipPriceInfo = array();
        $rateInfoArray = array();
        $confKey = ConfigConstants::$shippingRate; //out put is "shipping_rate"
        $rateInfo = $this->configDao->getShipInfoByConfKey($confKey); //get the shipping rate price from DB
        for ($i = 0; $i < count($rateInfo); $i++){
            $rateInfoArray[$rateInfo[$i]['conf_key']] = $rateInfo[$i]['conf_value']; //format the array to save the shipping key and value    
        }
        $itemInfoArray = $this->productItemDao->getByIds($itemIds); //get the item info by item id
            $shippingPrice = 0; //init the data
            $rate = $rateInfoArray[$confKey . "_" . $shippingMethodKey]; //get the each shipping rate price
            for($j = 0; $j < count($itemInfoArray); $j++){ //while the all order item
                $freeShippingStatus = $itemInfoArray[$j]['free_shipping_status']; //get the shipping status,1 is can free shipping goods
                $shipType = $itemInfoArray[$j]['shipping_type']; //get the item ship type
                $weight = $itemInfoArray[$j]['weight']; //get the item weight
                $qty = $qtyArray[$j]; //get the item quantity
                //if the shipping method is free shipping and this item can free shipping
                if($freeShippingStatus == 1 && $freeShippingMethod == $shippingMethodKey){ //$freeShippingStatus==1 is this item can free shipping 
                    $shippingPrice += 0;
                }else{//the shipping method can not free shipping,$freeShippingMethod != $shippingMethodKey
                    if($freeShippingStatus == 1 && $isFreeShippingArray[$j] > 0){//$isFreeShippingArray[$j]==0 then can not free shipping  
                    	$shippingPrice += 0;//this item can free shipping and the coupon set the item also can free shipping
                    }else{
                    	$shippingPrice += self::countShipFeeByTypeAndWeight($shipType, $weight, $qty, $rate); //return the ship price of one item
                    }
                	
                }
            }
            //$shippingPrice = number_format($shippingPrice, 2);
            $desc = "";
            $shipPriceInfo = array("shippingExpectKey" => $shippingMethodKey, 
                                                       "shippingPrice" => $shippingPrice, 
                                                       "desc" => $desc, 
                                                       "unit" => UNIT);
        return $shipPriceInfo;
		
	}
	
	public function countShipFeeByTypeAndWeight($shipType, $weight, $qty, $rate){
		$shipPrice = 0;
		if($shipType == 1){
			$shipPrice = $weight*$qty*$rate;
		}else if($shipType == 2){
			$shipPrice = $weight*$qty;
		}else if($shipType == 3){
			$shipPrice = 0;
		}
		return $shipPrice;
	}
	 
}


